package control
{
	import control.panel.PanelControl;
	
	import events.PlayEvent;
	
	import flash.events.Event;
	
	import view.components.ModulLoader;
	import view.panel.BrokenPanel;
	import global.GameState;
	
	public class BrokenPanelControl extends PanelControl
	{
		private var modulLoader:ModulLoader;
		
		public function BrokenPanelControl()
		{
			initPanel();
			initLoader();
			initListener();
		}
		
		private function initListener():void
		{
			_model.playerModel.addEventListener(PlayEvent.BROKE,onBroke);
			modulLoader.addEventListener(Event.COMPLETE,onComplete);
		}
		
		private function onComplete(event:Event):void
		{
			modulLoader.content.linkTutor = GameState.getTutorLink();
			modulLoader.content.linkShop = GameState.getShopLink();
			modulLoader.content.linkInvite = GameState.getInviteLink();
		}
		
		private function onBroke(event:PlayEvent):void
		{
			open();
		}
		
		private function initLoader():void
		{
			modulLoader = new ModulLoader(420,260);
			modulLoader.y = 8;
			_view.brokenPanel.addChild(modulLoader);
		}
		
		private function initPanel():void
		{
			_view.brokenPanel = new BrokenPanel(this);
			
		}
		
		public function open():void
		{
			if ( !modulLoader.isComplete && !modulLoader.isLoading)
			{
				modulLoader.load("resource/broke.swf");
			}
			_view.brokenPanel.open();
		}
	}
}